/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.cbr.similarity;

import java.util.ArrayList;
import java.util.List;
import jcolibri.exception.NoApplicableSimilarityFunctionException;
import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
import my.cbr.database.model.HatcheryCompanyID;
import my.cbr.database.model.Vaccine;

/**
 *
 * @author Geir Ole
 */
public class ListSimilarity implements LocalSimilarityFunction{

    public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException {
        if(!isApplicable(caseObject, queryObject))
            return 0.0;
        List caseO;
        List queryO;
        
        try{
            caseO = (List) caseObject;
            queryO = (List) queryObject;
        }catch(Exception e) {
            return 0.0;
        }
        
        int denominator = caseO.size();
        if(queryO.size() > denominator)
            denominator = queryO.size();
        
        int hits = 0;
        
        for(Object v1 : caseO) {
            for(Object v2 : queryO)
            {
                if(v1.equals(v2)) {
                    hits++;
                }   
            }
        }
        if(denominator==0)
            return 0.0;
        return (double)hits / denominator;
    }
    
    public boolean isApplicable(Object caseObject, Object queryObject) {
        return (caseObject instanceof List)&&(queryObject instanceof List);
    }
    
    public static void main(String[] args) {
        ListSimilarity listSimilarity = new ListSimilarity();
        List v1 = new ArrayList<Vaccine>();
        List v2 = new ArrayList<Vaccine>();
        Object v = new Vaccine(1, "1");
        v1.add(v);
        v2.add(v);
        v = new Vaccine(2, "2");
        v1.add(v);
        try {
            System.out.println(listSimilarity.compute(v1, v2));
        } catch (NoApplicableSimilarityFunctionException noApplicableSimilarityFunctionException) {
        }
        
        v1 = new ArrayList<HatcheryCompanyID>();
        v2 = new ArrayList<HatcheryCompanyID>();
        v = new HatcheryCompanyID(1);
        v1.add(v);
        v2.add(v);
        v = new HatcheryCompanyID(2);
        //v1.add(v);
        try {
            System.out.println(listSimilarity.compute(v1, v2));
        } catch (NoApplicableSimilarityFunctionException noApplicableSimilarityFunctionException) {
        }
    }
}
